package com.tbit.uqbike.client.controller;

import com.tbit.common.entity.TempPowerCheckResult;
import com.tbit.uqbike.client.business.temp_power.service.TempPowerService;
import com.tbit.uqbike.client.constant.RedisConstant;
import com.tbit.uqbike.client.pojo.Result;
import com.tbit.uqbike.client.pojo.User;
import com.tbit.uqbike.client.service.RedisService;
import com.tbit.uqbike.client.service.TokenService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;

/**
 * 禁行区
 *
 * @Author:BUGTian
 * @DATE: 2023/5/22
 */
@RequestMapping("/accountSpike")
@RestController
@Slf4j
public class AccountSpikeController {

    @Autowired
    private RedisService redisService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private TempPowerService tempPowerService;

    /**
     * 检测是否在禁行区内
     */
    @GetMapping("/checkSpike")
    public Result<Boolean> checkSpike(@NotNull String machineNO) {
        return Result.success(redisService.get(RedisConstant.REDIS_MACHINE_FORBIDDEN_ID + machineNO) != null);
    }

    /**
     * 检查是否允许临时通电
     */
    @GetMapping("/checkTempPower")
    public Result<Boolean> checkTempPower(@NotNull String token, Double lon, Double lat, @RequestParam(defaultValue = "2") Integer mapType) {
        User user = tokenService.getUser(token);
        return Result.success(tempPowerService.canTempPower(user.getUserId(), lon, lat, mapType, false).getCanTempPower());
    }

    /**
     * 检查是否允许临时通电（提供给其他端使用，不鉴权）
     */
    @GetMapping("/rpc/checkTempPower")
    public Result<TempPowerCheckResult> checkTempPower(@NotNull Integer userId, Double lon, Double lat, @RequestParam(defaultValue = "2") Integer mapType) {
        return Result.success(tempPowerService.canTempPower(userId, lon, lat, mapType, true));
    }
}
